package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.DevOrProdType;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.user.UserLoginRequest;
import com.yupi.springbootinit.model.dto.user.UserRegisterRequest;
import com.yupi.springbootinit.model.dto.user.UserUpdateRequest;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import static com.yupi.springbootinit.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = {DevOrProdType.devString},allowCredentials = "true")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录接口
     *
     * @param userLoginRequest
     * @param request
     * @return 用户信息
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> UserLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        //需要判断前端传过来的参数是否为空
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO user = userService.UserLogin(userLoginRequest,request);
        return ResultUtils.success(user);
    }

    /**
     * 注册接口
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> UserRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if(userRegisterRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long result = userService.UserRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * 获取当前登录用户接口
     * @param request
     * @return
     */
    @GetMapping("/current")
    public BaseResponse<UserVO> GetCurrentUser(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        UserVO currentUser = (UserVO) userObj;

        if(currentUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        // todo 实时更新用户的状态
//        Long id = currentUser.getId();
//        User user = userService.getById(id);
//        UserVO userVO = new UserVO();
//        BeanUtils.copyProperties(user,userVO);

        return ResultUtils.success(currentUser);
    }

    /**
     * 写入地址接口
     * @param address
     * @param request
     * @return
     */
    @PostMapping("/address/{address}")
    public BaseResponse<Boolean> WriteAddress(@PathVariable String address, HttpServletRequest request){
        UserVO loginUser = userService.getLoginUser(request);
        boolean result = userService.writeAddress(address,loginUser);
        return ResultUtils.success(result);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> UpdateUser(@RequestBody UserUpdateRequest userUpdateRequest,HttpServletRequest request){
        if(userUpdateRequest == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        UserVO loginUser = userService.getLoginUser(request);
        boolean result = userService.updateUser(userUpdateRequest,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 退出登录接口
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> UserLogoutController(HttpServletRequest request) {
        //需要判断前端传过来的参数是否为空
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        int result = userService.UserLogout(request);
        return ResultUtils.success(result);
    }
}
